# !/usr/bin python3                                 
# encoding    : utf-8 -*-                            
# @author     :   hmh                               
# @software   : PyCharm      
# @file       :   main.py
# @Time       :   2022/8/26 11:24

import pandas as pd
import jieba.analyse
import numpy as np
from step1_Extract_keywards import getKeywords_textrank
from step2_Time_transform import time_trans
from step2_Time_transform import time_cha
from step3_similarity_calculation import Jaccrad
from step4_Highlight_2min import highlight

def main():
	#关键词提取
	dataFile = (r'D:\desktop\crm_8-24时剩余告警\crm3\8-24时CRM3级警告数据剩余告警_有序.xlsx')
	data = pd.read_excel(dataFile)
	result = getKeywords_textrank(data, 10)
	#result.to_excel(r"D:\desktop\step1_Extract_keywards.xlsx", index=False)

	#时间戳转换
	timestamp1 = time_trans(data)

	#相邻告警时间差计算
	cha = time_cha(data)
	#cha.to_excel(r'D:/Desktop/cha.xlsx',index=False)

	#计算文本相似度
	idList, keyList, FtimeList = data['id'], data['key'], data['Ftime']
	ids, keys, Ftimes, jaccard_coefficient = [], [], [], []
	for index in range(len(idList)-1):
	text_a = '%s' % (keyList[index])  # 文本1
	text_b = '%s' % (keyList[index+1])  # 文本2
	jaccard_coefficient = Jaccrad(text_a, text_b)
	#print(jaccard_coefficient)

	# value = " ".join(jaccard_coefficient)
	# data['jaccard_coefficient'] = jaccard_coefficient
	# types = pd.DataFrame(jaccard_coefficient)
	# data.to_excel(r'D:/Desktop/values.xlsx', index=False)

	#筛选出时间差小于120s，相似度大于0.8的告警，返回对应(即index),标黄
	df = pd.read_excel(r'D:\desktop\crm_8-24时剩余告警\crm3\8-24时CRM3级警告数据剩余告警_有序.xlsx')
	a = df[['id']][(df['cha'] <= 120) & (df['jaccard_coefficient'] >= 0.8)]
	a.to_excel(r'D:\desktop\id_2min_cong.xlsx', index=False)
	#筛选出时间差小于24h，相似度大于0.8的告警（从），返回对应(即index)，标红
	b = df[['id']][(df['cha'] <= 86400) & (df['jaccard_coefficient'] >= 0.8)]
	b.to_excel(r'D:\desktop\id_86400_cong.xlsx', index=False)
	#筛选出时间差小于24h，首次出现告警（主），标粉
	c = df[['id']][(df['cha'] <= 86400) & (df['jaccard_coefficient'] >= 0.8)]-1
	c.to_excel(r'D:\desktop\id_2min_zhu.xlsx', index=False)

	c_1 = pd.read_excel(r'D:\desktop\id_2min_cong.xlsx')
	c_2 = pd.read_excel(r'D:\desktop\id_86400_cong.xlsx')
	c_3 = pd.read_excel(r'D:\desktop\id_2min_zhu.xlsx')
	data.style.apply(highlight, axis=1).to_excel(r'D:\desktop\highlight.xlsx', index=False)


if __name__ == '__main__':
    main()